<?php

class Homecontroller {

    public function invoke() {
        include_once LIBRARIES_ENTITIES_DATABASE;
        include_once LIBRARIES_ENTITIES_HANG_HOA;
        $dir = dirname(__FILE__);
        $xtpl = new XTemplate($dir . DS . "view" . DS . "index.php");
        $dshanghoa = $this->getListHangHoa();
        if ($dshanghoa != null) {            
            foreach ($dshanghoa as $hanghoa) {
                $xtpl->assign('INDEX', INDEX);
                $xtpl->assign('TEN_NGUOI_DANG', 'Nguyễn Hữu Phước');
                $xtpl->assign('TIEU_DE', $hanghoa->getHH_TEN());
                $xtpl->assign('HINH_ANH', $hanghoa->getHH_HINHANH());
                $xtpl->assign('GIA', number_format($hanghoa->getHH_DONGIA(),0,",","."));
                $xtpl->assign('MAU_SAC', $hanghoa->getHH_MAUSAC());
                $xtpl->assign('HH_ID', $hanghoa->getHH_ID());
                $xtpl->parse('main.dshanghoa');
            }            
            $xtpl->parse('main');
            $xtpl->out('main');
        } else {
            echo '<div class = "alert alert-warning" role = "alert">
            Chưa có sản phẩm nào
            </div>';
        }
    }

    public function getListHangHoa() {
        $db = new Database();
        try {
            $hanghoa = new M_Hang_hoa();
            $hanghoa = $hanghoa->getList($db);
            return $hanghoa;
        } catch (Exception $exc) {
            echo "Lỗi: " . $exc->getTraceAsString();
            exit();
        }
    }

}
